/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.UserSalaryMonthDetail;
import org.springblade.modules.admin.entity.UserSalaryRules;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.admin.entity.UserSalaryMonth;
import org.springblade.modules.admin.vo.UserSalaryMonthVO;
import org.springblade.modules.admin.wrapper.UserSalaryMonthWrapper;
import org.springblade.modules.admin.service.IUserSalaryMonthService;
import org.springblade.core.boot.ctrl.BladeController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 控制器
 *
 * @author ysq
 * @since 2025-05-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/usersalarymonth")
@Api(value = "工资-绩效管理", tags = "工资-绩效管理")
public class UserSalaryMonthController extends BladeController {

	private final IUserSalaryMonthService userSalaryMonthService;

	/**
	 * 自定义分页
	 */
	@PostMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入userSalaryMonth")
	public R<IPage<UserSalaryMonthVO>> page(@RequestBody UserSalaryMonthVO userSalaryMonth) {
		Query query = new Query();
		query.setSize(userSalaryMonth.getSize());
		query.setCurrent(userSalaryMonth.getCurrent());
		IPage<UserSalaryMonthVO> pages = userSalaryMonthService.selectUserSalaryMonthPage(Condition.getPage(query), userSalaryMonth);
		return R.data(pages);
	}


	@PostMapping("down/excel")
	@ApiOperation(value = "下载Excel", notes = "下载Excel")
	public void downExcel(HttpServletResponse response) throws IOException {
		List<UserSalaryMonth> list = userSalaryMonthService.list(Wrappers.<UserSalaryMonth>lambdaQuery().orderByAsc(UserSalaryMonth::getCreateTime));
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList("绩效月份","绩效模板","绩效人数","维保绩效合计","创建人","创建时间");
			rows.add(title);
			for (UserSalaryMonth userSalaryMonth : list) {
				List<String> row  = new ArrayList<>(120);
				row.add(Convert.toStr(userSalaryMonth.getSalaryMonth()));
				row.add(Convert.toStr(userSalaryMonth.getTemplateName()));
				row.add(Convert.toStr(userSalaryMonth.getUserCounts()));
				row.add(Convert.toStr(userSalaryMonth.getSalaryAllPrices()));
				row.add(Convert.toStr(userSalaryMonth.getCreateUserName()));
				row.add(DateUtil.format(userSalaryMonth.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
				rows.add(row);
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (
			Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}

	/**
	 * 功能描述: 发送响应流方法
	 */
	private void setResponseHeader(HttpServletResponse response, String fileName) {
		try {
			try {
				fileName = new String(fileName.getBytes(), "ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			response.setContentType("application/octet-stream;charset=ISO8859-1");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.addHeader("Pargam", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
